package com.attcatstudy.sync;

import lombok.extern.slf4j.Slf4j;

/**
 * @author attcat
 * ---做一个有趣的程序员
 * @date 2022-11-03 21:14
 * @description
 */
@Slf4j
public class SyncDemo {
    private  volatile static  int counter = 0;

    public static void increment(){
        counter++;
    }

    public static void decrement(){
        counter--;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread  T1=new Thread(()->{
            for(int i=0;i<5000;i++){
                increment();
            }
        },"T1");


        Thread  T2=new Thread(()->{
            for(int i=0;i<5000;i++){
                decrement();
            }
        },"T2");

        T1.start();
        T2.start();
        T1.join();
        T2.join();
        log.info("{}",counter);
    }

}
